package com.woniuxy.controller;

import com.woniuxy.dao.RepairRecordMapper;
import com.woniuxy.pojo.*;
import com.woniuxy.service.RepairRecordService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.RepairRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RepairRecordController extends BaseController {
    @Autowired
    private RepairRecordService repairRecordService;
    @Autowired
    private RepairRecordMapper repairRecordMapper;

    @GetMapping(value = "/repairRecords")
    public DataGridView selectRefuelingRecordsByProperty(RepairRecordVo repairRecordVo) {
        return repairRecordService.selectRepairRecordsByProperty(repairRecordVo);
    }
    /**
     * 根据id删除维修记录
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/repairRecords/{id}")
    public ResultDto deleteRepairRecordById(@PathVariable("id") int id) {
        try {
           this.repairRecordService.deleteRepairRecordById(id);
            return ResultDto.DELETE_REP_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_REP_FAILURE;
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping(value = "/repairRecords/deleteBathRepairRecord")
    public ResultDto removeRepairRecord(@RequestParam("ids[]") Integer[] ids) {
        try {
            this.repairRecordService.removeRepairRecord(ids);
            return ResultDto.DELETE_REP_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_REP_FAILURE;
        }
    }
    @GetMapping("/repairRecords/cars.do")
    public List<Car> cars() {
        return repairRecordMapper.selectAllCars();
    }
    @GetMapping("/repairRecords/users.do")
    public List<User> users() {
        return repairRecordMapper.selectAllUsers();
    }
    @GetMapping("/repairRecords/repairs.do")
    public List<CurrentUnit> repairs() {
        return repairRecordMapper.selectAllRepair();
    }
    @GetMapping("/repairRecords/repairTypes.do")
    public List<Dictionary> repairTypes() {
        return repairRecordMapper.selectAllRepairType();
    }
    @PostMapping(value = "/repairRecords.do")
    public ResultDto addRepairRecord(RepairRecord repairRecord) {
        boolean flag = this.repairRecordService.insertRepairRecord(repairRecord);
        if (flag) {
            return ResultDto.REP_ADD_SUCCESS;
        } else {
            return ResultDto.REP_ADD_FAIL;
        }
    }
    @PostMapping(value = "/repairRecords/update.do")
    public ResultDto updateRepairRecord(RepairRecord repairRecord) {
        boolean flag = this.repairRecordService.updataRepairRecord(repairRecord);
        if (flag) {
            return ResultDto.REP_UPDATE_SUCCESS;
        } else {
            return ResultDto.REP_UPDATE_FAIL;
        }
    }
}
